<template>
  <div id='logoin'>
    <!-- 导航栏 -->
    <nav-bar />
    <img id="imgUrl" :src="logo" alt="">
    <!-- 表单域 -->
    <van-form @submit="login">
      <van-field
        v-model="loginList.phone"
        name="手机号"
        label="手机号"
        placeholder="请输入手机号"
        :rules="[{ required: true, validator, message: '请输入正确的手机号' }]"
      />
      <van-field
        v-model="loginList.password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit" submit-on-enter
          >登录</van-button
        >
      </div>
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          color="#FF6702"
          @click="$router.push('/register')"
          native-type="submit"
          >注册</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import navBar from "@/components/navBar";
import { loginInfo } from "@/util/axios";
import { mapActions } from "vuex";
//轻提示
import { Toast } from "vant";
export default {
  data() {
    return {
      logo: require("../assets/images/login/orange.png"),
      loginList: {
        phone: "", //手机号
        password: "", //密码
      },
    };
  },
  components: {
    navBar,
  },
  methods: {
    ...mapActions(["changeUserAction"]),
    // 校验函数返回 true 表示校验通过，false 表示不通过
    validator(val) {
      return /1\d{10}/.test(val);
    },
    //注册
    login() {
      loginInfo(this.loginList).then((res) => {
        if (res.code == 200) {
          //把登录返回的个人信息全部存储到vuex中
          this.changeUserAction(res.list);
          Toast.success(res.msg);
          this.$router.push("/mine");
        } else {
          Toast.fail(res.msg);
        }
      });
    },
  },
};
</script>

<style lang="" scoped>
#imgUrl{
  width: 1.82rem;
  margin: 2.44rem auto 0.02rem;
}
#logoin{
  width: 100vw;
  height: 100vh;
  background-color: #fff;
}
</style>